/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checador;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author lynx
 */
public class AccesoXML {
    
    private final String nombre;
    private DocumentBuilderFactory docFactory;
    private DocumentBuilder docBuilder;
    private Document doc;
    private NodeList nodos;
    private Node raiz;
    
    Node n;

    public AccesoXML(String nombre) throws ParserConfigurationException, SAXException, IOException {
        this.nombre = nombre;
        docFactory = DocumentBuilderFactory.newInstance();
        docBuilder = docFactory.newDocumentBuilder();
        doc = docBuilder.parse(nombre);
        nodos = doc.getFirstChild().getChildNodes();
    }

    NodeList obtenNodos() {
        return nodos;
    }
    
    void nuevo(String id, String nombre, String apellido){
        Node raiz = doc.getFirstChild();
        Element nuevo = doc.createElement("dato");
        nuevo.setAttribute("id", id);
        Element nom = doc.createElement("nombre");
        nom.appendChild(doc.createTextNode(nombre));
        Element ape = doc.createElement("apellido");
        ape.appendChild(doc.createTextNode(apellido));       
        nuevo.appendChild(nom);
        nuevo.appendChild(ape);
        raiz.appendChild(nuevo);
        guardar();
    }
    
 
    
    
    Node buscar(String id){
        //Node n;
        for(int i=0; i<nodos.getLength(); i++){
            n = nodos.item(i);
            if(n.getAttributes().item(0).getNodeValue() == null ? id == null : n.getAttributes().item(0).getNodeValue().equals(id)){
                return n;
            }
        }
        return null;
    }
    
    public void guardar(){
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File(nombre));
                transformer.transform(source, result);
        } catch (TransformerException ex) {
            Logger.getLogger(AccesoXML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void eliminar(Node n) {
        Node raiz = doc.getFirstChild();
        raiz.removeChild(n);
        guardar();
    }
    public void agregarRegistros(String Nombre,String id,String EOS, String horaActual){
        
        Node raiz = doc.getFirstChild();
        Element reg = doc.createElement("Registro");
        reg.setAttribute("id", id);
        reg.setAttribute("Nombre", Nombre);
        reg.setAttribute("se", EOS);
        reg.setAttribute("hora",horaActual );
        raiz.appendChild(reg);
        guardar();
        
    }
    
   Node primerNodo(){
         
    raiz=doc.getFirstChild();    
    
    return raiz;
   }
   
}
  
    
